import os
import time
import math
from os.path import join, exists
import cv2
import numpy as np
import h5py
from common import shuffle_ in_ unison_ scary, logger, createDir, process Image
from common import getDataF romTxt
from utils import show_ landmark, flip, rotate
TRAIN= '/home/tyd/ 下载/ deep_ landmark/ con face- data '
OUTPUT = '/home/tyd/ 下载/ deep_ landmark/ mydataset/mytrain'
if not exists (OUTPUT):
os . mkdir(OUTPUT)
assert( exists (TRAIN) and exists (OUTPUT))
def generate_ hdf5(ftxt, output, fname, argument=False):
data = getDataFromTxt(ftxt)
F_ imgs = []
F_ landmarks = []
EN_ imgs = []
EN_ landmarks = []
NM_ imgs = []
NM_ landmarks = []
for (imgPath, bbox, landmarkGt) in data: 
ing = cv2.imread(imgPath, cv2.CV_ LOAD_ IMAGE_ _GRAYSCALE)
assert(img is not None )
logger("process %5" 8 imgPath)
#F
f_ bbox = bbox. subBBox(-0.05， 1.05, -0.05, 1.05)
f_ face = img[f_ bbox. top:f_ bbox. bottom+1,f_ bbox.left:f_ bbox. right+1]
or (imgPath, bbox, landmarkGt) in data:
img = cv2. imread(imgPath, cv2.CV_ LOAD_ IMAGE _GRAYSCALE)
assert(img is not None) 
logger("process %s" % ingPath)
f_ bbox = bbox. subBBox(-0.05，1.05， -0.05， 1.05)
f_ face = img[f_ bbox. top:f_ bbox. bottom+1,f_ bbox.left:f_ bbox. right+1]
## data argument
if argument and np . random. rand() > -1:
# flip
face_ flipped, landmark_ flipped = flip(f_ face, landmarkGt)
face_ flipped = cv2. resize(face_ _flipped， (39, 39))
F_ imgs. append(face_ flipped. reshape((1, 39, 39))
F_ Landmarks.append (landmark flipped. reshape(10))
for (imgPath, bbox, landmarkGt) in data:
ing = cv2. imread(imgPath, cv2.CV_ _LOAD_ IMAGE_ _GRAYSCALE)
assert(img is not None )
logger("process %s" & imgPath)
#F
f_ bbox = bbox . subBBox(-0.05, 1.05， -0.05， 1.05)
f_ face = ing[f_ bbox. top:f_ bbox . bottom+1,f_ bbox. left:f_ bbox. right+1]
## data argument
if argument and np. random. rand() > -1:
# flip
face_ flipped, landmark_ flipped = flip(f_ face, landmarkGt)
face_ flipped = cv2. resize(face_ flipped, (39， 39))
F_ imgs . append(face_ flipped. reshape((1, 39, 39)))
F_ landmarks.append( landmark_ flipped . reshape(10))
nm_ face = f_ face[8:, :]
f_ face = f_ face. reshape((1, 39, 39))
f_ landmark = LandmarkGt . reshape((10))
F_ imgs. append(f_ face)
F_ landmarks. append(f_ landmark)
1f argument and np. random. rand() > 0.5:
## fip
face_ flipped, landmark _flipped = flip(nm_ face, landmarkGt)
en_ landmark = landmarkGt[:3, :].reshape((6))
EN_ imgs . append(en_ face)
EN_ landmarks . append (en_ landmark)
# NM
# nm_ bbox = bbox. subBBox(-0.05，1.05， 0.18， 1. 05]
# nm. face = img[nm. bbox. top:nm. bbox. bottom+1,om boox.Leftinu bbox. righ
## data aroument
if argument and np. random. rand() > 0.5:
face_ flipped, landmark_ flipped = flip(nm_ _face, landmarkGt)
face_ _flipped = cv2. resize(face_ _flipped, (31, 39)). reshape((1, 31, 39))
landmark_ flipped = landmark flipped[3: ，:]. reshape((6))
EN_ imgs . append(face_ flipped)
EN_ landmarks . append (landmark_ _flipped)
en_ face = cv2.resize(en_ face, (31, 39)).reshape((1, 31, 39))
en_ landmark = landmarkGt[:3， :]. reshape((6))
EN_ imgs 。append(en_ face )
EN_ landmarks. append (en_ landmark)
if argument and np. random. rand() > 0.5:
### flip
face_ flipped, landmark flipped = flip(nm_ face, landmarkGt)
face_ flipped = cv2. resize(face_ flipped, (31, 39)). reshape((1, 31, 39))
landmark_ _flipped = landmark_ flippeg[2:, :]. reshape((6))
NM_ imgs . append(face_ flipped)
NM landmarks . append (landmark flipped)
nm_ face = cv2.resize(nm_ face, (31, 39)). reshape((1, 31, 39))
nm_ Landmark = landmarkGt[2:, :]. reshap((6))
NM imgs . append I nm face
nm Landmark = landmarkGt[2:, :]. reshape((6))
NM imgs . append (nm face )
NM_ landmarks . append(nm_ landmark)

F_ imgs, F_ landmarks = np.asarray(F_ imgs), np. asarray(F_ landmarks )
EN_ imgs, EN_ landmarks = np. asarray(EN_ imgs)，np. asarray(EN_ landmarks)
NM_ imgs， NM_ landmarks = np. asarray(NM imgs)。np. asarray(NM_ Landmarks )
F_ ings = processImage(F_ imgs)
shuffle_ in_ unison_ scary(F_ _imgs, F_ landmarks )
EN_ imgs = processImage(EN_ _imgs)
shuffle_ in_ unison_ scary(EN_ imgs， EN_ landmarks )
NM_ imgs = processImage(NM_ _imgs )
base = join(OUTPUT, '1 F')
createDir( base)
output = join(base, fname)
logger( "generate 8s" 8 output)
with h5py. File(output，'W') as h5:
h5['data'] = F_ imgs . astype (np. float32)
h5[' landmark'] = F_ landmarks . astype (np . float32)
base = join(OUTPUT, 'I. EN' )
createDir(base )
output = join(base, fname )
logger("generate %s"暑output)
with h5py. File(output，'W') as h5:
h5['data'] = EN imgs. astype (np. float32)
h5['landmark'] = EN_ landmarks . astype(np. float32)
# nose and mouth
base = join(OUTPUT, '1. NM' )I
createDir( base)
output = join(base, fname)
logger("generate %s" % output)
with h5py. File(output，'w') as h5:
h5['data'] = NM_ imgs. astype(np. float32)
logger("generate %5" % output)
with h5py. File(output，'W') as h5:
h5['data'] = NM_ imgs. astype(np. float32)
h5['landmark'] = NM_ landmarks . astype (np. float32)
if name == ‘ main ‘ :
h5_ path = ' /home/tyd/下载/deep. .landmark/ mydataset'
train_ txt = join(TRAIN，'trainImageList. txt')
generate_ hdf5(train_ txt，OUTPUT,' train.h5', argument=True )
test_ txt = join(TRAIN，'testImagelist. txt')
generate_ hdf5(test_ _txt, OUTPUT, ' test.h5')
with open( join(OUTPUT, '1_ F/train.txt'), ‘W') as fd:
fd.write(h5_ path+' train/1_ F/train. h5' )
with open( join(OUTPUT, '1_ EN/train.txt'), 'W') as fd:
fd. write(h5_ path+' train/1_ EN/train.h5' )
with open( join(OUTPUT, '1_ NM/train.txt'), 'W') as fd:
fd.write(h5_ path+' train/1_ NM/ train. h5' )
with open( join(OUTPUT, '1_ F/test.txt'), ‘W') as fd:
fd. write(h5_ path+' train/1_ EN/train.h5')
with open( join(OUTPUT, '1 NM/train. txt'), 'W') as fd:
fd.write(h5_ path+' train/1_ NM/train. h5" )
with open( join(OUTPUT, '1_ F/test. txt'),‘W') as fd:
fd.write(h5_ path+ ' train/1_ F/test.h5' )
with open(join(OUTPUT，'1_ EN/test.txt'), 'W') as fd:
fd. write (h5_ path+' train/1_ EN/test.h5')
with open( join(OUTPUT, '1. NM/test.txt'),‘W') as fd:
fd.write(h5_ path+ ' train/1_ NM/test.h5' )